<?php
$auth = Auth::getAuth('current_user');
$db = new Database;
$fs = new functions;
$search = new Search();
$starred = new Starred($db);
$userID = $auth['id'];
 if(isset($_POST['action'])){

     $date = $fs->currentDateTime();

     switch($_POST['action']){
        case "loadData":
              //  $returnJson = array();
              //  $forms_array = $search->getCompaniesForm();
              //  array_unshift($forms_array, array("id"=>"0","form_name"=>"Announcement"));
              //  $start = $_POST['start'];
              //  foreach ($forms_array as $forms) {
              //     $id =  $forms['id'];
              //     $request_array = $search->getStarredRequests($id,$start,10);
              //     foreach ($request_array as $request) {
              //       array_push($request, $forms);
                    // array_push($request, array("user_image"=>post::avatarPic("tbuser",$request['requestor_id'],"30","30","small","avatar")));
              //       array_push($returnJson, $request);
              //     }
              // }
              // echo json_encode($returnJson);
              // $starred->getStarred();
              $search_value = $_POST['search_value'];
              $start = $_POST['start'];
              $data = $starred->getStarred($search_value,"LIMIT $start, 10");
              echo json_encode($data);
            break;
        case "countData":
            // $ctr = 0;
            // $forms_array = $search->getCompaniesForm();

            // $start = $_POST['start'];
            // array_unshift($forms_array, array("id"=>"0","form_name"=>"Announcement"));  
            // foreach ($forms_array as $forms) {
            //     $id =  $forms['id'];
            //     $request_array = $search->countStarredRequests($id);
            //     $ctr+=$request_array;
            //     //print_r($request_array);
            // }
            $data = $starred->getStarred("","");
            $ctr = count($data);
            echo $ctr;
          break;
        case "updateData":

            //UPDATED STARRED HERE
            $data_starred = $_POST['data_starred'];
            $data_id = $_POST['data_id'];
            if($data_starred==0){
                //insert
              $tablename = $_POST['tablename'];
              $strSqlCheckExist = "SELECT * FROM tbstarred WHERE tablename = '". $tablename ."' AND data_id = '". $data_id ."' AND  user_id = '". $auth['id'] ."'";
              
              if($db->query($strSqlCheckExist,"numrows")==0){
                $insert = array("tablename"=>$tablename,
                            "data_id"=>$data_id,
                            "user_id"=>$auth['id']);
                $id = $db->insert("tbstarred",$insert);
                $json = array("status"=>"added",
                    "message"=>"Added to your starred","type"=>"success","id"=>$id);
              }
            }else{
                //delete
                $where = array("id"=>$data_starred);
                $db->delete("tbstarred",$where);
                $json = array("status"=>"removed",
                    "message"=>"Removed to your starred","type"=>"success","id"=>$data_starred);
                //echo $data_starred;
            }
            echo json_encode($json);
            break;
     }
    
 }
Class Starred extends Auth{
  /*Properies*/
  public $db = "";
  public $auth = "";


  // /* Functions/Methods */

  public function __construct($database){
    $this->db = $database;
    $this->auth = $this->getAuth('current_user');
  }


  public function getStarred($search_value,$limit){
    $userId = $this->auth['id'];
    $queryStr = "SELECT * FROM tbstarred WHERE user_id = $userId ORDER BY tablename ".$limit;
    $getStarred = $this->db->query($queryStr,"array");
    $returnArray = array();
    $post = new post();
    $functions = new functions();
    $otherUser = "";
    $data_id = "";
    foreach ($getStarred as $value) {
      if($value['type']==0){
        $strSqlData = "SELECT frm.*,u.first_name,u.last_name,u.middle_name, ws.id as ws_id, ws.form_name, ws.form_json FROM ".$value['tablename']." 
                      frm LEFT JOIN tbuser u ON frm.Requestor=u.id , tb_workspace ws WHERE frm.id = '".$value['data_id']."'  
                      AND ws.form_table_name='".$value['tablename']."' AND (u.first_name LIKE '%". $search_value ."%' OR u.last_name LIKE '%". $search_value ."%' 
                      OR CONCAT_WS(' ',u.first_name,last_name) LIKE '%". $search_value ."%')";
        // echo $strSqlData."\n";
        $otherUser = "Requestor";
        $getData = $this->db->query($strSqlData,"row");
        $data_id = $getData['ID'];
      }else if($value['type']==1){
        $strSqlData = "SELECT *,frm.id as data_id FROM ".$value['tablename']." frm LEFT JOIN tbuser u ON frm.postedBy = u.id WHERE frm.id = ".$value['data_id']." AND (u.first_name LIKE '%". $search_value ."%'
                      OR u.last_name LIKE '%". $search_value ."%' 
                      OR CONCAT_WS(' ',u.first_name,last_name) LIKE '%". $search_value ."%')";
        $otherUser = "postedBy";
        $getData = $this->db->query($strSqlData,"row");
        if($getData){
          $data_id = $getData['data_id'];
          $getData['form_name'] = "Announcement";
          $getData['DateCreated'] = $getData['date_posted'];
          $getData['link'] = $functions->base_encode_decode("encrypt",$value['data_id']);
        }
      }
      
      if($getData){
        $getData['type'] = $value['type'];
        $getData['starred_id'] = $value['id'];
        $getData['data_id'] = $data_id;
        $getData['user_image'] = $post->avatarPic("tbuser",$getData[$otherUser],"30","30","small","avatar");
        array_push($returnArray, $getData);
      }
    }

    return $returnArray;  
  }

}
?>
